/*
 * @Descripttion: 
 * @Author: cuidm
 * @Date: 2022-05-10 16:49:46
 * @LastEditors: cuidm
 * @LastEditTime: 2022-08-11 10:08:48
 */
import { useState, useEffect } from 'react';
import cmdbres from '@/data/cmdbres';
import request from '../../utils/request';
const defaultValue = {
    associateType: {},
    associateTypeData: []
};
export async function queryAssociateType() {
    try {
        let res = await request({
            ...cmdbres.queryAssociateType,
            data:{
                pageSize: 9999,
                pageNumber: 1,
            }
        });
        if (res && res.code == 200) {
            let obj = {}
            if(res.data) {
                res.data.map(item=> obj[item.associateTypeCode] = item.name);
            }

            return {
                associateType: obj,
                associateTypeData:res.data
            }
        }
        return defaultValue;
    } catch (error) {
        console.log(error);
        return defaultValue;
    }
}

function useModelAssociateType() {
    const [loading, setLoading] = useState(false);
    const [data, setData] = useState({});

    useEffect(() => {
        setLoading(true);
        setData({});
        queryAssociateType()
            .then((res) => {
                setLoading(false);
                setData(res);
            })
            .catch((e) => {
                setLoading(false);
                setData(defaultValue);
                throw e;
            });
    }, []);
    return [data, loading];
}

export default useModelAssociateType;

